package silce

import (
	"fmt"
	"time"
	"unsafe"

	"github.com/gookit/color"
)

type ArrAsParam struct {
}

func (r ArrAsParam) ArrHandle(arr [2]int) {
	color.Blueln("IsEqual", len(arr), cap(arr), arr)
	arr[0], arr[1] = arr[1], arr[0]
	color.Yellowln("IsEqual", len(arr), cap(arr), arr)
}
func (r ArrAsParam) ArrHandleSelf(arr *[2]int) {
	color.Blueln("ArrHandleSelf", len(arr), cap(arr))
	arr[0], arr[1] = arr[1], arr[0]
	color.Yellowln("ArrHandleSelf", len(arr), cap(arr), arr)

}
func (r ArrAsParam) SliceHandle(arr ...int) {
	color.Blueln("SliceHandle", len(arr), cap(arr), arr)
	if len(arr) >= 2 {
		arr[0], arr[1] = arr[1], arr[0]
	} else {
		color.Redln("SliceHandle, len", len(arr))
	}
	color.Yellowln("SliceHandle", len(arr), cap(arr), arr)
}
func (r ArrAsParam) SliceHandleSelf(arr []int) {
	color.Blueln("SliceHandleSelf in", len(arr), cap(arr), arr)
	arr[0], arr[1] = arr[1], arr[0]
	color.Yellowln("SliceHandleSelf out", len(arr), cap(arr), arr)

}

type sProblem struct {
}

func (s sProblem) Append(num int) {
	from := []int{1, 2, 3}
	arr := make([]int, 0, num*num*3)
	now := time.Now()
	defer func() { fmt.Println("append time", time.Since(now).Nanoseconds()) }()
	for i := 0; i < num*num; i++ {
		arr = append(arr, from...)
	}
	fmt.Println("append", len(arr), cap(arr))
}
func (s sProblem) Copy(num int) {
	from := []int{1, 2, 3}
	arr := make([]int, num*num*3)
	now := time.Now()
	defer func() { fmt.Println("copy time", time.Since(now).Nanoseconds()) }()
	for i := 0; i < num*num; i++ {
		copy(arr[(i)*3:(i+1)*3], from)
	}
	fmt.Println("copy", len(arr), cap(arr))
}

func SliceInfo[S ~[]E, E any](s S) {
	color.Redf("value:%v;p:%v,%v:%v Sizeof:%v\n", s, &s, len(s), cap(s), unsafe.Sizeof(s))
}
